/*
 * zserver.cpp
 *
 *  Created on: 24 avr. 2012
 *      Author: Florian.B
 */

#include "zserver.h"
#include "../client/client.h"
#include <stdlib.h>

void toggledConnection(GtkCellRendererToggle *cell_renderer, gchar *gpath,
	gpointer user_data)
    {
    GtkTreePath * path = gtk_tree_path_new_from_string(gpath);
    ZServer *zServer = user_data;
    GtkTreeIter iter;
    gboolean b;

    if (path)
	{
	Client *client;
	gtk_tree_model_get_iter(GTK_TREE_MODEL(zServer->store), &iter, path);
	gtk_tree_path_free(path);

	gtk_tree_model_get(GTK_TREE_MODEL(zServer->store), &iter,
		CLIENT_COLUMN_SER, &client, CON_COLUMN_SER, &b, -1);
	gtk_tree_store_set(zServer->store, &iter, CON_COLUMN_SER, !b, -1);
	if (b)
	    {
	    quit_Client(client);
	    }

	else if (connexion_Client(client) == -1)
	    {
	    GtkWidget *dialog;
	    gtk_tree_store_set(zServer->store, &iter, CON_COLUMN_SER, b, -1);
	    dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
		    GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,
		    "Erreur de connexion au serveur %s!", client->host);
	    gtk_dialog_run(GTK_DIALOG(dialog));
	    gtk_widget_destroy(dialog);
	    }
	else
	    {
	    GtkWidget *dialog;
	    waitHello_Client(client);
	    dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
		    GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
		    "Connexion au serveur %s réussi !", client->host);
	    gtk_dialog_run(GTK_DIALOG(dialog));
	    gtk_widget_destroy(dialog);

	    }
	}
    (void) cell_renderer;
    }

void treeSelectionActivated_ZServer(GtkTreeView *tree_view, GtkTreePath *path,
	GtkTreeViewColumn *column, gpointer data)
    {
    GtkTreeIter iter;
    gboolean b;
    Client *client;
    ZServer *zServer = data;
    if (path)
	{
	gtk_tree_model_get_iter(GTK_TREE_MODEL(zServer->store), &iter, path);
	gtk_tree_model_get(GTK_TREE_MODEL(zServer->store), &iter,
		CON_COLUMN_SER, &b, CLIENT_COLUMN_SER, &client, -1);
	if (isHaveList_Client(client) == CLIENT_DONT_HAVE)
	    {
	    ClientReturn cRet = getListFile_Client(client);
	    if (cRet != CLIENT_SUCCESS)
		{
		GtkWidget *dialog;
		dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL,
			GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Erreur : %s",
			getMessageReturn_Client(cRet));
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
		return;
		}
	    }
	changeListFile_ZFile(zServer->zFile, client);

	}
    else
	{
	changeListFile_ZFile(zServer->zFile, NULL);
	}
    (void) tree_view;
    (void) column;
    }

void treeSelectionChanged_ZServer(GtkTreeSelection *selection,
	gpointer user_data)
    {
    GtkTreeIter iter;
    GtkTreeModel *model;
    ZServer *zServer = (ZServer *) user_data;
    if (!gtk_tree_selection_get_selected(selection, &model, &iter))
	changeListFile_ZFile(zServer->zFile, NULL);
    }

ZServer *create_ZServer(ZFile *zFile)
    {
    GtkTreeViewColumn *column;
    GtkCellRenderer *renderer;
    GtkTreeSelection *select;
    ZServer *s = (ZServer *) malloc(sizeof(ZServer));
    s->zFile = zFile;
    s->store = gtk_tree_store_new(N_COLUMNS_SER, G_TYPE_STRING, G_TYPE_BOOLEAN,
	    G_TYPE_POINTER);
    s->tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(s->store));
    initialize_List(&(s->lClient),
	    (void(*)(void **, void * const )) createCopy_Client, (void(*)(
		    void **)) delete_Client, NULL, NULL);
    renderer = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes("Serveur", renderer,
	    "text", SERVER_COLUMN_SER, NULL);
    gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
    gtk_tree_view_append_column(GTK_TREE_VIEW(s->tree), column);

    renderer = gtk_cell_renderer_toggle_new();
    column = gtk_tree_view_column_new_with_attributes("Connecte", renderer,
	    "active", CON_COLUMN_SER, NULL);
    g_signal_connect(renderer, "toggled", G_CALLBACK(toggledConnection), s);

    gtk_tree_view_column_set_resizable(GTK_TREE_VIEW_COLUMN(column), TRUE);
    gtk_tree_view_append_column(GTK_TREE_VIEW(s->tree), column);

    /* The column can be clicked by the user */
    gtk_tree_view_set_headers_clickable(GTK_TREE_VIEW(s->tree), TRUE);

    /* Setup the selection handler */
    select = gtk_tree_view_get_selection(GTK_TREE_VIEW(s->tree));
    gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);

    /* Create a scroll widget to resize the tree */
    s->box = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(s->box), s->tree);

    g_signal_connect(G_OBJECT(s->tree), "row-activated", G_CALLBACK(
	    treeSelectionActivated_ZServer), s);
    g_signal_connect(G_OBJECT(select), "changed", G_CALLBACK(
	    treeSelectionChanged_ZServer), s);
    return s;
    }

GtkWidget *getContainer_ZServer(ZServer *zServer)
    {
    return zServer->box;
    }

void addServer_ZServer(ZServer *zServer, const gchar *host, const gchar *port,
	const gchar *nom)
    {
    Client client;
    Client *clientList;
    GtkTreeIter iter;
    initialize_Client(&client, host, port, nom);
    insertLast_List(&(zServer->lClient), &client);
    clientList = getDataLast_List(&(zServer->lClient));
    gtk_tree_store_append(zServer->store, &iter, NULL);
    gtk_tree_store_set(zServer->store, &iter, SERVER_COLUMN_SER, host,
	    CLIENT_COLUMN_SER, clientList, -1);
    testament_Client(&client);
    }

void closeAllConnection_ZServer(ZServer *zServer)
    {
    List *list = &(zServer->lClient);
    if (getSize_List(list) > 0)
	{
	first_List(list);
	do
	    {
	    quit_Client(getDataCurrent_List(list));
	    }
	while (next_List(list) == LIST_SUCCESS);
	}
    }
